package com.taotao.portal.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.pojo.TaotaoResult;
import com.taotao.pojo.TbItemDesc;
import com.taotao.portal.pojo.ItemInfo;
import com.taotao.portal.service.ItemService;
import com.taotao.utils.HttpClientUtil;
import com.taotao.utils.JsonUtils;
@Service
public class ItemServiceImpl implements ItemService{
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${ITME_INFO_URL}")
	private String ITME_INFO_URL;
	@Value("${ITEM_DESC_URL}")
	private String ITEM_DESC_URL;
	

	@Override
	public ItemInfo getItemById(Long itemId) {
		String json=HttpClientUtil.doGet(REST_BASE_URL+ITME_INFO_URL+itemId);
		if(!StringUtils.isBlank(json)){
			TaotaoResult result=JsonUtils.jsonToPojo(json, TaotaoResult.class);
			if(result.getStatus()==200){
				ItemInfo tbItem=JsonUtils.jsonToPojo(JsonUtils.objectToJson(result.getData()), ItemInfo.class);
				//ItemInfo tbItem=(ItemInfo) result.getData();
				return tbItem;
			}
			
		}
		return null;
	}

	@Override
	public String getItemDescById(Long itemId) {
		try {
			//查询商品描述
			String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_DESC_URL + itemId);
			//转换成java对象
			TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, TaotaoResult.class);
			if (taotaoResult.getStatus() == 200 &&  taotaoResult.getData()!=null) {
				TbItemDesc itemDesc = (TbItemDesc) taotaoResult.getData();
				//取商品描述信息
				String result = itemDesc.getItemDesc();
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

}
